package com.codeape.number.ui.main.fragment.divination;

import android.content.ContentValues;
import android.content.Intent;
import android.os.AsyncTask;
import android.widget.Toast;

import com.codeape.number.base.mvvm.command.Action;
import com.codeape.number.base.mvvm.command.Command;
import com.codeape.number.ui.AnnotationActivity;
import com.codeape.number.util.DatabaseUtils;
import com.codeape.number.util.Utils;

import java.lang.ref.WeakReference;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * DivinationViewModel
 *
 * @author LiuPeiming
 * Created at 2018/10/10 3:45
 */
public class DivinationViewModel {

    private DivinationFragment fragment;

    private static String mCalculateResult;

    DivinationViewModel(DivinationFragment fragment) {
        this.fragment = fragment;
    }

    /**
     * 计算
     */
    public Command toCalculationCommand = new Command(new Action() {
        @Override
        public void run() {
            if (fragment.binding.editText1.getText().length() == 3 && fragment.binding.editText2.getText().length() == 3 && fragment.binding.editText3.getText().length() == 3) {

                mCalculateResult = Utils.Calculate(fragment.binding.editText1.getText().toString(), fragment.binding.editText2.getText().toString(), fragment.binding.editText3.getText().toString());

                new operationTask(fragment).execute();

            } else {
                Toast.makeText(fragment.getActivity(), "请输入正确数字！", Toast.LENGTH_SHORT).show();
            }
        }
    });

    /**
     * 清空
     */
    public Command toEmptyCommand = new Command(new Action() {
        @Override
        public void run() {
            fragment.binding.editText1.setText("");
            fragment.binding.editText2.setText("");
            fragment.binding.editText3.setText("");
            fragment.binding.editText1.requestFocus();// editText1获取焦点
        }
    });

    private static class operationTask extends AsyncTask<String, Object, Long> {

        private final WeakReference<DivinationFragment> mFragment;

        private operationTask(DivinationFragment mFragment) {
            this.mFragment = new WeakReference<>(mFragment);
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Long doInBackground(String... strings) {
            //插入数据
            ContentValues cValue = new ContentValues();//实例化常量值
            cValue.put("resultCode", mCalculateResult);
            cValue.put("time", new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(new Date(System.currentTimeMillis())));//获取当前时间
            DatabaseUtils.getRecordsDb().insert("records", null, cValue);//调用insert()方法插入数据
            return null;
        }

        @Override
        protected void onPostExecute(Long aLong) {
            super.onPostExecute(aLong);
            DivinationFragment fragment = mFragment.get();
            fragment.startActivity(new Intent(fragment.getActivity(), AnnotationActivity.class)
                    .putExtra("result", DatabaseUtils.getCalculationResult(mCalculateResult)));
            fragment.getActivity().sendBroadcast(new Intent().setAction("action.refreshData"));//刷新历史记录
        }
    }

}